﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ReflexTérkép.Tests.dto;

namespace ReflexTérkép.Tests
{
    [TestClass]
    public class MappingToEnumTests : TestBase
    {
        [TestMethod]
        public void Can_Map_Enum_Values_To_Enum_Properties()
        {
            // Arrange
            const int id = 1;
            const string firstName = "Jimbo";
            const string lastName = "Smith";
            const Gender gender = Gender.Male;

            var dictionary = new Dictionary<string, object>
                {
                    { "Id", id },
                    { "FirstName", firstName },
                    { "LastName", lastName },
                    { "Gender", gender }
                };

            // Act
            var customer = ReflexMapper.Map<Person>(dictionary);

            // Assert
            Assert.IsNotNull(customer);
            Assert.IsTrue(customer.Id == id);
            Assert.IsTrue(customer.FirstName == firstName);
            Assert.IsTrue(customer.LastName == lastName);
            Assert.IsTrue(customer.Gender == gender);

        }

        [TestMethod]
        public void Can_Map_Integer_Values_To_Enum_Properties()
        {
            // Arrange
            const int id = 1;
            const string firstName = "Jimbo";
            const string lastName = "Smith";
            const Gender gender = Gender.Male;

            var dictionary = new Dictionary<string, object>
                {
                    { "Id", id },
                    { "FirstName", firstName },
                    { "LastName", lastName },
                    { "Gender", 2 }
                };

            // Act
            var customer = ReflexMapper.Map<Person>(dictionary);

            // Assert
            Assert.IsNotNull(customer);
            Assert.IsTrue(customer.Id == id);
            Assert.IsTrue(customer.FirstName == firstName);
            Assert.IsTrue(customer.LastName == lastName);
            Assert.IsTrue(customer.Gender == gender); 
        }

        [TestMethod]
        public void Can_Map_String_Values_To_Enum_Fields()
        {
            // Arrange
            const int id = 1;
            const string firstName = "Jimbo";
            const string lastName = "Smith";
            const Gender gender = Gender.Male;

            var dictionary = new Dictionary<string, object>
                {
                    { "Id", id },
                    { "FirstName", firstName },
                    { "LastName", lastName },
                    { "Gender", "2" }
                };

            // Act
            var customer = ReflexMapper.Map<Person>(dictionary);

            // Assert
            Assert.IsNotNull(customer);
            Assert.IsTrue(customer.Id == id);
            Assert.IsTrue(customer.FirstName == firstName);
            Assert.IsTrue(customer.LastName == lastName);
            Assert.IsTrue(customer.Gender == gender); 
        }

        [TestMethod]
        public void Can_Map_String_Values_To_Enum_Properties()
        {
            // Arrange
            const int id = 1;
            const string firstName = "Jimbo";
            const string lastName = "Smith";
            const Gender gender = Gender.Male;

            var dictionary = new Dictionary<string, object>
                {
                    { "Id", id },
                    { "FirstName", firstName },
                    { "LastName", lastName },
                    { "Gender", "2" }
                };

            // Act
            var customer = ReflexMapper.Map<Person>(dictionary);

            // Assert
            Assert.IsNotNull(customer);
            Assert.IsTrue(customer.Id == id);
            Assert.IsTrue(customer.FirstName == firstName);
            Assert.IsTrue(customer.LastName == lastName);
            Assert.IsTrue(customer.Gender == gender);
        }

        [TestMethod]
        public void Can_Map_Null_Values_To_Nullable_Enum_Fields()
        {
            // Arrange
            const int id = 1;
            const string firstName = "Jimbo";
            const string lastName = "Smith";
            const Gender gender = Gender.Male;
            MaritalStatus? maritalStatus = null;

            dynamic person = new ExpandoObject();
            person.Id = id;
            person.FirstName = firstName;
            person.LastName = lastName;
            person.Gender = gender;
            person.MaritalStatus = maritalStatus;

            // Act
            Person customer = ReflexMapper.MapDynamic<Person>(person);

            // Assert
            Assert.IsNotNull(customer);
            Assert.IsTrue(customer.Id == id);
            Assert.IsTrue(customer.FirstName == firstName);
            Assert.IsTrue(customer.LastName == lastName);
            Assert.IsTrue(customer.Gender == gender);
            Assert.IsTrue(customer.MaritalStatus == maritalStatus);
        }

        [TestMethod]
        public void Can_Map_Null_Values_To_Nullable_Enum_Properties()
        {
            // Arrange
            const int id = 1;
            const string firstName = "Jimbo";
            const string lastName = "Smith";
            const Gender gender = Gender.Male;
            MaritalStatus? maritalStatus = null;

            dynamic person = new ExpandoObject();
            person.Id = id;
            person.FirstName = firstName;
            person.LastName = lastName;
            person.Gender = gender;
            person.MaritalStatus = maritalStatus;

            // Act
            Person customer = ReflexMapper.MapDynamic<Person>(person);

            // Assert
            Assert.IsNotNull(customer);
            Assert.IsTrue(customer.Id == id);
            Assert.IsTrue(customer.FirstName == firstName);
            Assert.IsTrue(customer.LastName == lastName);
            Assert.IsTrue(customer.Gender == gender);
            Assert.IsTrue(customer.MaritalStatus == maritalStatus);
        }
    }
}
